package com.qiyi.player.core.video.file.flashp2pfile
{
   import flash.events.EventDispatcher;
   import com.qiyi.player.core.model.IMovie;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.core.video.decoder.IDecoder;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.core.model.events.MovieEvent;
   import com.qiyi.player.core.video.file.actors.datatile.MediaData;
   import com.qiyi.player.loader.vod.P2PFileLoader;
   import flash.net.NetStream;
   import com.qiyi.player.core.model.impls.Segment;
   import com.iqiyi.components.global.GlobalStage;
   import com.qiyi.player.core.model.ISkipPointInfo;
   import com.qiyi.player.core.model.def.SkipPointEnum;
   import flash.events.Event;
   import com.qiyi.player.base.logging.Log;
   
   public class FlashP2PFile extends EventDispatcher implements IFlashP2PFile
   {
      
      public function FlashP2PFile() {
         this._log = Log.getLogger("com.qiyi.player.core.video.file.p2pfile.P2PFile");
         super();
         this._log.debug("FlashP2PFile has been created!");
      }
      
      private var _P2P:Object;
      
      private var _movie:IMovie;
      
      private var _holder:ICorePlayer;
      
      private var _decoder:IDecoder;
      
      private var _log:ILogger;
      
      public function initialize(param1:ICorePlayer, param2:IMovie, param3:IDecoder) : void {
         this._holder = param1;
         this._movie = param2;
         this._decoder = param3;
         this._movie.addEventListener(MovieEvent.Evt_UpdateSkipPoint,this.onUpdateSkipPoint);
         this.createP2PFile();
      }
      
      public function get holder() : ICorePlayer {
         return this._holder;
      }
      
      public function get movie() : IMovie {
         return this._movie;
      }
      
      public function get bufferLength() : int {
         if(this._P2P)
         {
            return this._P2P.bufferLength;
         }
         return 0;
      }
      
      public function get eof() : Boolean {
         if(this._P2P)
         {
            return this._P2P.eof;
         }
         return false;
      }
      
      public function get P2P() : Object {
         return this._P2P;
      }
      
      public function setTriggerEvent(param1:Object) : void {
         var var_28:Object = param1;
         if(this._P2P)
         {
            try
            {
               this._P2P.setTriggerEvent(var_28);
            }
            catch(error:Error)
            {
            }
         }
      }
      
      public function sequenceReadData() : MediaData {
         var _loc1_:Object = null;
         var _loc2_:MediaData = null;
         if(this._P2P)
         {
            _loc1_ = this._P2P.sequenceReadData();
            if(_loc1_)
            {
               _loc2_ = new MediaData();
               _loc2_.headers = _loc1_.headers;
               _loc2_.bytes = _loc1_.bytes;
               return _loc2_;
            }
         }
         return null;
      }
      
      public function sequenceReadDataFrom(param1:int) : MediaData {
         var _loc2_:Object = null;
         var _loc3_:MediaData = null;
         if(this._P2P)
         {
            _loc2_ = this._P2P.sequenceReadDataFrom(param1);
            if(_loc2_)
            {
               _loc3_ = new MediaData();
               _loc3_.headers = _loc2_.headers;
               _loc3_.bytes = _loc2_.bytes;
               return _loc3_;
            }
         }
         return null;
      }
      
      public function seek(param1:int) : void {
         if(this._P2P == null)
         {
            this.createP2PFile();
         }
         this._P2P.seek(param1);
      }
      
      public function clear() : void {
         if(this._P2P)
         {
            this._P2P.removeEventListener("Evt_p2pFailed",this.onP2PFailed);
            this._P2P.clear();
            this._P2P = null;
         }
      }
      
      public function prepareSwitchMediaData() : void {
         if(this._P2P)
         {
            this._P2P.removeEventListener("Evt_p2pFailed",this.onP2PFailed);
            this._P2P.clear();
            this._P2P = null;
         }
      }
      
      public function destroy() : void {
         if(this._movie)
         {
            this._movie.removeEventListener(MovieEvent.Evt_UpdateSkipPoint,this.onUpdateSkipPoint);
         }
         if(this._P2P)
         {
            this._P2P.removeEventListener("Evt_p2pFailed",this.onP2PFailed);
            this._P2P.clear();
            this._P2P = null;
         }
      }
      
      private function createP2PFile() : void {
         if(this._P2P)
         {
            this._P2P.removeEventListener("Evt_p2pFailed",this.onP2PFailed);
            this._P2P.clear();
         }
         this._P2P = P2PFileLoader.instance.getFile(this.createVideoInfo(),this._decoder as NetStream);
         this._P2P.addEventListener("Evt_p2pFailed",this.onP2PFailed);
         this.updateSkipPointsData();
         try
         {
            this._P2P.fragmentMode = this._holder.runtimeData.openSelectPlay;
         }
         catch(error:Error)
         {
         }
      }
      
      private function createVideoInfo() : Object {
         var _loc2_:Segment = null;
         var _loc3_:* = 0;
         var _loc4_:* = 0;
         var _loc5_:Array = null;
         var _loc6_:* = 0;
         var _loc1_:Object = null;
         if(this._movie)
         {
            _loc2_ = null;
            _loc3_ = 0;
            if(this._holder.strategy)
            {
               _loc2_ = this._movie.getSegmentByTime(this._holder.strategy.getStartTime());
               if(_loc2_)
               {
                  _loc3_ = _loc2_.index;
               }
            }
            _loc1_ = new Object();
            _loc1_.coreRuntimeData = this._holder.runtimeData;
            _loc1_.info = 
               {
                  "duration":this._movie.duration,
                  "trailerTime":this._movie.trailerTime,
                  "channelID":this._movie.channelID,
                  "albumID":this._movie.albumId,
                  "uuid":this._holder.uuid,
                  "curSegmentIndex":_loc3_
               };
            _loc1_.log = this._log;
            _loc1_.stage = GlobalStage.stage;
            _loc4_ = this._movie.curDefinition.segmentCount;
            _loc5_ = new Array(_loc4_);
            _loc6_ = 0;
            while(_loc6_ < _loc4_)
            {
               _loc2_ = this._movie.curDefinition.findSegmentAt(_loc6_);
               _loc5_[_loc6_] = 
                  {
                     "url":_loc2_.url,
                     "totalBytes":_loc2_.totalBytes,
                     "vid":_loc2_.vid,
                     "index":_loc2_.index,
                     "startTime":_loc2_.startTime,
                     "totalTime":_loc2_.totalTime
                  };
               _loc6_++;
            }
            _loc1_.segments = _loc5_;
         }
         return _loc1_;
      }
      
      private function updateSkipPointsData() : void {
         var skipPointInfoArr:Array = null;
         var count:int = 0;
         var skipPointInfo:ISkipPointInfo = null;
         var item:Object = null;
         var i:int = 0;
         if((this._P2P && this._movie) && (this._movie.curDefinition) && (this._movie.curDefinition.metaIsReady))
         {
            skipPointInfoArr = [];
            count = this._movie.skipPointInfoCount;
            skipPointInfo = null;
            item = null;
            i = 0;
            i = 0;
            while(i < count)
            {
               skipPointInfo = this._movie.getSkipPointInfoAt(i);
               if(skipPointInfo.skipPointType == SkipPointEnum.ENJOYABLE)
               {
                  item = new Object();
                  item.type = 6;
                  item.startTime = skipPointInfo.startTime;
                  item.endTime = skipPointInfo.endTime;
                  skipPointInfoArr.push(item);
               }
               i++;
            }
            if(skipPointInfoArr.length > 0)
            {
               try
               {
                  this._P2P.fragmentList(skipPointInfoArr);
               }
               catch(error:Error)
               {
               }
            }
         }
      }
      
      private function onUpdateSkipPoint(param1:MovieEvent) : void {
         this.updateSkipPointsData();
      }
      
      private function onP2PFailed(param1:Event) : void {
         dispatchEvent(new Event("Evt_p2pFailed"));
      }
   }
}
